// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Load/TLOAD2Parser.h"

#include <algorithm>
#include <cstdint>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/ITLOAD2Service.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (TLOAD2);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/TLOAD2/TLOAD2.md>
*/
// 以下需要修改
enum TLOAD2Key : uint8_t
{
    TLOAD2_SID      = 2,
    TLOAD2_EXCITEID = 3,
    TLOAD2_DELAY    = 4,
    TLOAD2_TYPE     = 5,
    TLOAD2_T1       = 6,
    TLOAD2_T2       = 7,
    TLOAD2_F        = 8,
    TLOAD2_P        = 9,
    TLOAD2_C        = 12,
    TLOAD2_B        = 13,
    TLOAD2_US0      = 14,
    TLOAD2_VS0      = 15,
};
namespace
{
    SG::DataStructure::Common::I64 getTLOAD2Type (std::string& typeStr)
    {
        boost::trim (typeStr);
        boost::to_upper (typeStr);
        if (typeStr == "12")
        {
            return 12;
        }
        if (typeStr == "13")
        {
            return 13;
        }
        switch (typeStr[0])
        {
        case '0':
            return 0;
        case '1':
            return 1;
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case 'L':
            return 0;
        case 'D':
            return 1;
        case 'V':
            return 2;
        case 'A':
            return 3;
            break;
        default:
            throw std::runtime_error ("getTLOAD2Type have a bad input:");
            throw std::runtime_error (typeStr + "\n");
            break;
        }
    }
}  // namespace

SG::DataStructure::Common::Status BDF::BULKDATA::ParseTLOAD2 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::TLOAD2Data tempdata;
    // TODO(bug 001) m_dominId的正确填写，当前默认domainId设置为1
    tempdata.m_domainId = 1;

    // 当前词条的索引，初始值为1
    int curIDKey = 1;
    // 遍历分割后的词条信息

    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前词条是否为空白
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;  // 如果是空白，跳过当前词条
        }

        // 将词条转换为科学计数法格式
        bool isfloat = covertToScientificNotation (curStrKey);

        switch (curIDKey)
        {
        case TLOAD2_SID:
            tempdata.m_sId = convertToI64 (curStrKey);
            break;
        case TLOAD2_EXCITEID:
            tempdata.m_DAREA = convertToI64 (curStrKey);
            break;
        case TLOAD2_DELAY:
            isfloat ? tempdata.m_T = convertToReal (curStrKey) : tempdata.m_DELAY = convertToI64 (curStrKey);
            break;
        case TLOAD2_TYPE:
            tempdata.m_TYPE = getTLOAD2Type (curStrKey);
            break;
        case TLOAD2_T1:
            tempdata.m_T1 = convertToReal (curStrKey);
            break;
        case TLOAD2_T2:
            tempdata.m_T2 = convertToReal (curStrKey);
            break;
        case TLOAD2_F:
            tempdata.m_F = convertToReal (curStrKey);
            break;
        case TLOAD2_P:
            tempdata.m_P = convertToReal (curStrKey);
            break;
        case TLOAD2_C:
            tempdata.m_C = convertToReal (curStrKey);
            break;
        case TLOAD2_B:
            tempdata.m_B = convertToReal (curStrKey);
            break;
        case TLOAD2_US0:
            tempdata.m_U0 = convertToReal (curStrKey);
            break;
        case TLOAD2_VS0:
            tempdata.m_V0 = convertToReal (curStrKey);
            break;
        default:
            // Handle unknown key
            break;
        }
        // 处理完当前词条后，索引递增
        curIDKey++;
    }

    return dbServiceFactory->get<ITLOAD2Service> ()->Add (tempdata);
}